#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMimeData>
#include <QUrl>
#include <QDropEvent>
#include <QByteArray>
#include <QDragEnterEvent>
#include <QFileInfo>
#include <QScreen>

#include <QDebug>
#include "playtoolbar.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    setAcceptDrops(true);


    QList<QScreen*> screens = QGuiApplication::screens();
    QRect screenRect = screens.at(0)->geometry();

    m_playToolBar = new playToolBar(this);
    connect(m_playToolBar, &playToolBar::fullScreen, this, [=](bool full){
        if (full) {
             setGeometry(screenRect);
        } else {
            QSize videoSize = m_AVCodecControler->GetMediaWidthHeight();
            if (videoSize.width() != this->frameSize().width()) {
                QRect rect(0, 0, videoSize.width(), videoSize.height());
                rect.moveCenter(screenRect.center());
                setGeometry(rect);
            }
        }
    });
}

MainWindow::~MainWindow()
{
    releasePlayer();
    delete ui;
}

void MainWindow::openMediaPlayer(const QString &filePath)
{
    if (filePath.isEmpty())
        return;

    QFileInfo mediaInfo(filePath);
    QString suffix = mediaInfo.suffix();

    if( suffix == "mp4" || suffix == "mkv" || suffix == "wmv" || suffix == "mov" || suffix == "avi"){
        m_mediaType = VIDEO;
    } else if(suffix == "mp3" || suffix == "m4a" || suffix == "wma"){
       m_mediaType = MUSIC;
    }

    releasePlayer();
    setupPlayer();

    if (m_AVCodecControler) {
        m_AVCodecControler->setVideoPathString(filePath);
        m_AVCodecControler->InitVideoCodec();

        m_playToolBar->setDuration(m_AVCodecControler->GetMediaTotalSeconds());
        m_playToolBar->setCurrentTimeStamp(0);
        m_playToolBar->raise();
    }

    QSize videoSize = m_AVCodecControler->GetMediaWidthHeight();

    m_nTotalMediaTime = m_AVCodecControler->GetMediaTotalSeconds();

    if (videoSize.width() != this->frameSize().width()) {
        resize(videoSize);
    }

    m_AVCodecControler->StartPlayVideo();
    m_AVCodecControler->setMuted(m_playToolBar->isMuted());
}

void MainWindow::releasePlayer()
{
    m_playToolBar->setAVCodecControler(nullptr);

    if (m_AVCodecControler) {
        m_AVCodecControler->StopPlayVideo();

        delete m_AVCodecControler;
        m_AVCodecControler = nullptr;
    }

    if (m_AVCodecControler)
        m_AVCodecControler->setUpdateVideoCallback(nullptr, 0);

    if(m_pOpenGLWidget)
    {
        delete m_pOpenGLWidget;
        m_pOpenGLWidget = nullptr;
    }

}

void MainWindow::setupPlayer()
{
    m_AVCodecControler = new xh_AVCodecControler(this);

    m_playToolBar->setAVCodecControler(m_AVCodecControler);

    m_AVCodecControler->setUpdateVideoCallback(updateVideoData, (unsigned long)this);

    m_pOpenGLWidget = new renderWidget(centralWidget());
    m_pOpenGLWidget->setAutoFillBackground(true);
    m_pOpenGLWidget->show();

    updateSize();
}

void MainWindow::updateVideoData(H264YUV_Frame* yuvFrame,unsigned long userData)
{
    if(userData == NULL || yuvFrame == NULL){
        return;
    }

    MainWindow* mainWindow = (MainWindow*)userData;
    if(mainWindow){
        mainWindow->UpdateYUVFrameData(yuvFrame);
    }
}

void MainWindow::UpdateYUVFrameData(H264YUV_Frame* yuvFrame)
{
    if(yuvFrame == nullptr) {
        return;
    }

    m_pOpenGLWidget->RendVideo(yuvFrame);
}

void MainWindow::updateCurrentPts(uint64_t pts)
{
    m_playToolBar->setCurrentTimeStamp(pts);
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
}

void MainWindow::dropEvent(QDropEvent *event)
{
    QUrl url = event->mimeData()->urls().first();
    if (url.isEmpty())
        return;

    openMediaPlayer(url.toLocalFile());
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    event->acceptProposedAction();
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    m_playToolBar->setGeometry(0, height() - m_playToolBar->height(), width(), m_playToolBar->height());

    updateSize();
}

void MainWindow::updateSize()
{
    if (m_AVCodecControler) {
        QSize size = m_AVCodecControler->GetMediaWidthHeight();
        size.scale(width(), height(), Qt::KeepAspectRatio);

        if (m_pOpenGLWidget) {
            m_pOpenGLWidget->setGeometry((width() - size.width())/2, (height() - size.height())/2, size.width(), size.height());
        }
    }
}

